from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_tarifEdit

class TarifEdit(QDialog, Ui_tarifEdit.Ui_tarifEditDlg):
    
  StyleSheet = """
QLineEdit[mandatory="true"] {
    background-color: rgb(255, 255, 127);
    color: darkblue;
}
"""

  def __init__(self, dbConn, tipe,  dataGlob, parent=None):
        super(TarifEdit, self).__init__(parent)
        self.setupUi(self)
        self.db = dbConn
        self.tipe = tipe
        self.dataGlob = dataGlob
        self.initData()
        
        self.mastModel = QSqlTableModel(self)
        self.mastModel.setTable("master_tarif")
        self.mastModel.setEditStrategy(QSqlTableModel.OnManualSubmit)
        
        self.lineedits = (self.code_le, self.cat_le)
        for lineEdit in self.lineedits:
            lineEdit.setProperty("mandatory", QVariant(True))
        self.setStyleSheet(TarifEdit.StyleSheet)
        
  @pyqtSignature("")
  def on_save_pb_clicked(self):
      if self.tipe == "add":
          if self.tambahData():
              self.close()
          else:
              QMessageBox.warning(self, "Master Tarif","Can not add new data.")
      else:
          if self.updateData():
              self.close()
          else:
              QMessageBox.warning(self, "Master Tarif","Can not update data")
      
  @pyqtSignature("")
  def on_cancel_pb_clicked(self):
      self.close()

  def tambahData(self):
      if not self.validasi():
          return
      self.mastModel.select()      
      QSqlDatabase.database().transaction()
      if not self.mastModel.insertRow(0):
          return
      self.mastModel.setData(self.mastModel.index(0, 0), QVariant(self.code_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 1), QVariant(self.cat_le.text()))
      if not self.mastModel.submitAll():
          return
      if not QSqlDatabase.database().commit():
          return
      return True

  def updateData(self):
      if not self.validasi():          
          return
      self.mastModel.setFilter("kode_jenis_tarif = '"+self.tipe+"'")
      self.mastModel.select()      
      QSqlDatabase.database().transaction()
      self.mastModel.setData(self.mastModel.index(0, 0), QVariant(self.code_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 1), QVariant(self.cat_le.text()))
      if not self.mastModel.submitAll():
          return
      if not QSqlDatabase.database().commit():
          return
      return True

  def initData(self):
      if self.tipe != "add":
          query = QSqlQuery(self.db)
          qStr = "select kode_jenis_tarif, nama_jenis_tarif from master_tarif where kode_jenis_tarif = '"+self.tipe+"'"
          query.exec_(qStr)
          query.next()
          self.code_le.setText(unicode(query.value(0).toString()))
          self.cat_le.setText(unicode(query.value(1).toString()))
      
  def validasi(self):
      valid = True
      for lineEdit in self.lineedits:
           if lineEdit.text().isEmpty():
               QMessageBox.warning(self, "Master Tarif","Please cek mandatory column (Yellow background)")
               return False
      return valid

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = TarifEdit()
    form.show()
    app.exec_()
